package cn.dankal.flyaudio.personal;

import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.ReasonBean;
import cn.dankal.flyaudio.R;

/**
 * 修改密码
 * */

public class ModifyPasswdActivity extends BaseActivity {


    //验证验证码返回的key
    @BindView(R.id.et_origin_passwd)
    EditText mEtOriginPasswd;
    @BindView(R.id.et_passwd)
    EditText mEtPasswd;
    @BindView(R.id.et_confirmpasswd)
    EditText mEtConfirmpasswd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modifypasswd;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("密码修改");
    }


    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        String originPasswd = mEtOriginPasswd.getText().toString();
        String passWd = mEtPasswd.getText().toString();
        String confirmPasswd = mEtConfirmpasswd.getText().toString();
        if (!StringUtil.checkPasswd(passWd) || passWd.length() < 6) {
            ToastUtils.showShort("请输入6-20数字、字母、符号，不允许特殊符号`'\\“ 空格");
            return;
        }

        if (!StringUtil.checkPasswd(confirmPasswd) || confirmPasswd.length() < 6) {
            ToastUtils.showShort("请输入6-20数字、字母、符号，不允许特殊符号`'\\“ 空格");
            return;
        }

        if (!passWd.equals(confirmPasswd)) {
            ToastUtils.showShort("密码不一致");
            return;
        }

        setPaseword(originPasswd, passWd, confirmPasswd);

    }

    /**
     * 设置密码
     */
    private void setPaseword(String originPasswd, String passWd, String confirmPasswd) {

        Map<String, String> map = new HashMap<String, String>(4) {{
                put("oldPassword", originPasswd);
                put("newPassword", passWd);
                put("confirmPassword", confirmPasswd);
                put("userId", DKUserManager.getUserInfo().getId());
            }
        };
        UserServiceFactory.resetPassword(map)
                .subscribe(new AbstractDialogSubscriber<ReasonBean>(this) {
                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        ToastUtils.showShort("密码更改成功");
                        finish();
                    }
                });
    }
}
